/*
 * tarm_iic.c
 *
 *  Created on: 2019年12月10日
 *      Author: tinnu
 */


#include <tarm_ctimer.h>
#include "board.h"
#include <stdio.h>

//*************** SYSTICK ****************************
volatile uint32_t g_systickCounter;
void SysTick_Handler(void)
{
    if (g_systickCounter != 0U)
    {
        g_systickCounter--;
    }
}

void SysTick_DelayTicks(uint32_t n)
{
	uint32_t temp,i=0;

#ifndef _SYSTICK_8DIV_
	SysTick->LOAD=(uint32_t)SystemCoreClock/1000;				//Ê±¼ä¼ÓÔØ(SysTick->LOADÎª24bit)
	SysTick->CTRL|=SysTick_CTRL_CLKSOURCE_Msk;
#else
	SysTick->LOAD=(uint32_t)SystemCoreClock/8000;
	SysTick->CTRL&=(~SysTick_CTRL_CLKSOURCE_Msk);
#endif

	for(i=0;i<n;i++)
	{
		SysTick->VAL =0x00;							//Çå¿Õ¼ÆÊýÆ÷
		SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;	//¿ªÊ¼µ¹Êý
		do
		{
			temp=SysTick->CTRL;
		}while((temp&0x01)&&!(temp&(1<<16)));		//µÈ´ýÊ±¼äµ½´ï
		SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;	//¹Ø±Õ¼ÆÊýÆ÷
		SysTick->VAL =0X00;       					//Çå¿Õ¼ÆÊýÆ÷
	}
}

void delay_ms(uint32_t n)
{
	SysTick_DelayTicks(n);
}

void delay_us(uint32_t n)
{
	for(int i=0; i<=n; i++)
		for(int j=0; j<=100; j++)
			__NOP();
}

//*************** CTIME0 ****************************
/* Multiple callback functions definition */
ctimer_callback_t timer_CTIMER0_callbackFunc[] = {CTIMER0_IntMa0CallBack, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
//100US
void CTIMER0_Init(void)
{
	const ctimer_config_t timer_CTIMER0_config = {
			.mode = kCTIMER_TimerMode,
			.input = kCTIMER_Capture_0,
			.prescale = 0
	};
	const ctimer_match_config_t timer_CTIMER0_Match_0_config = {
			.matchValue = 100,
			.enableCounterReset = true,
			.enableCounterStop = true,
			.outControl = kCTIMER_Output_NoAction,
			.outPinInitState = false,
			.enableInterrupt = true
	};
	//1-deinit
	CTIMER_Deinit(CTIMER0);
	//2-clock
	CLOCK_AttachClk(kFRO1M_to_CTIMER0);
	//3-CTIMER0 peripheral initialization
	CTIMER_Init(CTIMER0, &timer_CTIMER0_config);
	//4-Match channel 0 of CTIMER0 peripheral initialization
	CTIMER_SetupMatch(CTIMER0, kCTIMER_Match_0, &timer_CTIMER0_Match_0_config);
	CTIMER_RegisterCallBack(CTIMER0, timer_CTIMER0_callbackFunc, kCTIMER_MultipleCallback);
}

//XUS
void CTIMER0_InitXUs(uint32_t inpu_timeUs)
{
	const ctimer_config_t timer_CTIMER0_config = {
			.mode = kCTIMER_TimerMode,
			.input = kCTIMER_Capture_0,
			.prescale = 0
	};

	if(inpu_timeUs<100)
		inpu_timeUs=100;

	const ctimer_match_config_t timer_CTIMER0_Match_0_config = {
			.matchValue = inpu_timeUs,
			.enableCounterReset = true,
			.enableCounterStop = true,
			.outControl = kCTIMER_Output_NoAction,
			.outPinInitState = false,
			.enableInterrupt = true
	};
	//1-deinit
	CTIMER_Deinit(CTIMER0);
	//2-clock
	CLOCK_AttachClk(kFRO1M_to_CTIMER0);
	//3-CTIMER1 peripheral initialization
	CTIMER_Init(CTIMER0, &timer_CTIMER0_config);
	//4-Match channel 0 of CTIMER0 peripheral initialization
	CTIMER_SetupMatch(CTIMER0, kCTIMER_Match_0, &timer_CTIMER0_Match_0_config);
	CTIMER_RegisterCallBack(CTIMER0, timer_CTIMER0_callbackFunc, kCTIMER_MultipleCallback);
}

void CTIMER0_Reset(void)
{
	CTIMER0->TCR = 0x03;
	CTIMER0->TC = 0;
}

void CTIMER0_ChangeMatchValue(uint32_t inpu_value)
{
	const ctimer_match_config_t timer_CTIMER0_Match_0_config = {
			.matchValue = inpu_value,
			.enableCounterReset = true,
			.enableCounterStop = true,
			.outControl = kCTIMER_Output_NoAction,
			.outPinInitState = false,
			.enableInterrupt = true
	};

	CTIMER_SetupMatch(CTIMER0, kCTIMER_Match_0, &timer_CTIMER0_Match_0_config);
}

//*************** CTIME1 ****************************
/* Multiple callback functions definition */
ctimer_callback_t timer_CTIMER1_callbackFunc[] = {CTIMER1_IntMa0CallBack, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
void CTIMER1_Init(void)
{
	const ctimer_config_t timer_CTIMER1_config = {
			.mode = kCTIMER_TimerMode,
			.input = kCTIMER_Capture_0,
			.prescale = 0
	};
	const ctimer_match_config_t timer_CTIMER1_Match_0_config = {
			.matchValue = 100,
			.enableCounterReset = true,
			.enableCounterStop = true,
			.outControl = kCTIMER_Output_NoAction,
			.outPinInitState = false,
			.enableInterrupt = true
	};
	//1-deinit
	CTIMER_Deinit(CTIMER1);
	//2-clock
	CLOCK_AttachClk(kFRO1M_to_CTIMER1);
	//3- CTIMER1 peripheral initialization
	CTIMER_Init(CTIMER1, &timer_CTIMER1_config);
	//4- Match channel 0 of CTIME	R1 peripheral initialization
	CTIMER_SetupMatch(CTIMER1, kCTIMER_Match_0, &timer_CTIMER1_Match_0_config);
	CTIMER_RegisterCallBack(CTIMER1, timer_CTIMER1_callbackFunc, kCTIMER_MultipleCallback);
}

void CTIMER1_InitXUs(uint32_t inpu_timeUs)
{
	const ctimer_config_t timer_CTIMER1_config = {
			.mode = kCTIMER_TimerMode,
			.input = kCTIMER_Capture_0,
			.prescale = 0
	};

	if(inpu_timeUs<100)
		inpu_timeUs=100;

	const ctimer_match_config_t timer_CTIMER1_Match_0_config = {
			.matchValue = inpu_timeUs,
			.enableCounterReset = true,
			.enableCounterStop = true,
			.outControl = kCTIMER_Output_NoAction,
			.outPinInitState = false,
			.enableInterrupt = true
	};
	//1-deinit
	CTIMER_Deinit(CTIMER1);
	//2-clock
	CLOCK_AttachClk(kFRO1M_to_CTIMER1);
	//3- CTIMER1 peripheral initialization
	CTIMER_Init(CTIMER1, &timer_CTIMER1_config);
	//4- Match channel 0 of CTIMER1 peripheral initialization */
	CTIMER_SetupMatch(CTIMER1, kCTIMER_Match_0, &timer_CTIMER1_Match_0_config);
	CTIMER_RegisterCallBack(CTIMER1, timer_CTIMER1_callbackFunc, kCTIMER_MultipleCallback);
}

void CTIMER1_Reset(void)
{
	CTIMER1->TCR = 0x03;
	CTIMER1->TC = 0;
}

void CTIMER1_ChangeMatchValue(uint32_t inpu_value)
{
	const ctimer_match_config_t timer_CTIMER1_Match_0_config = {
			.matchValue = inpu_value,
			.enableCounterReset = true,
			.enableCounterStop = true,
			.outControl = kCTIMER_Output_NoAction,
			.outPinInitState = false,
			.enableInterrupt = true
	};

	CTIMER_SetupMatch(CTIMER1, kCTIMER_Match_0, &timer_CTIMER1_Match_0_config);
}

//***************** timer to counter the debug speed ************************
void CTIMER2_Init(void)
{
	const ctimer_config_t timer_CTIMER2_config = {
			.mode = kCTIMER_TimerMode,
			.input = kCTIMER_Capture_0,
			.prescale = 0
	};
	//1-deinit
	CTIMER_Deinit(CTIMER2);
	//2-clock
	CLOCK_AttachClk(kFRO1M_to_CTIMER2);
	//3- CTIMER1 peripheral initialization
	CTIMER_Init(CTIMER2, &timer_CTIMER2_config);
}


//***************** callback ************************
void CTIMER0_IntMa0CallBack(uint32_t flags)
{
//	static int i=0;
//	if(i>=10)
//	{
//		LED_BLUE_TOGGLE();
//		i=0;
//	}
//	else
//		i++;

	CTIMER_StartTimer(CTIMER0);
	Time0CallBack();

}

void CTIMER1_IntMa0CallBack(uint32_t flags)
{
//	static int i=0;
//	if(i>=10)
//	{
//		LED_BLUE_TOGGLE();
//		i=0;
//	}
//	else
//		i++;

	CTIMER_StartTimer(CTIMER1);
	Time1CallBack();
}

